<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增课程信息')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-course-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">课程名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">课程介绍：</label>
                <div class="col-sm-8">
                    <textarea name="introduce" class="form-control"></textarea>                    
                </div>
            </div>
            <div class="form-group">                
                <label class="col-sm-3 control-label" for="coursePicture">课程图片：</label>
                <input name="picture" id="picture" class="form-control" type="hidden">
                <div class="col-sm-8">                    
                    <input type="file" class="" name="coursePicture" id="coursePicture" accept="image/*"/>
                    <img src="" alt="" id="yl" width="40px" height="40px" display="none"/>
                    <button class="btn" type="button" id="upload">上传</button>
                </div>
            </div> 
            <div class="form-group">                 
                <label class="col-sm-3 control-label">课程分类名称：</label>
                 <input name="typeName" id="typeName" class="form-control" type="hidden">  
                <div class="col-sm-8">
                <select class="form-control m-b" name="typeId" id="typeId">                  
                   <option th:each="type : ${courseTypes}" th:text="${type.name}" th:value="${type.id}"></option>
                </select>   
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">课程价格：</label>
                <div class="col-sm-8">
                    <input name="price" class="form-control" type="text">
                </div>
            </div>            
            <div class="form-group">    
                <label class="col-sm-3 control-label">发布时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="publishTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                    </div>
                </div>
            </div>            
            <div class="form-group">    
                <label class="col-sm-3 control-label">课程主讲老师：</label>
                <div class="col-sm-8">
                <input name="teacherName" id="teacherName" class="form-control" type="hidden">
                <select class="form-control m-b" name="teacherId" id="teacherId">                  
                   <option th:each="teacher : ${teachers}" th:text="${teacher.name}" th:value="${teacher.id}"></option>
                </select>    
                </div>
            </div>            
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script type="text/javascript">
        
      
        var prefix = ctx + "course/course"
        $("#form-course-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
            	$("#typeName").val($("#typeId").find("option:selected").text());
            	$("#teacherName").val($("#teacherId").find("option:selected").text());
                $.operate.save(prefix + "/add", $('#form-course-add').serialize());
            }
        }

        $("input[name='publishTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });        
        
        //上传课程图片......
        $('#coursePicture').on('change', function() {
            var reader = new FileReader();
            reader.onload = function(e) {
                var src = e.target.result;
                //根据MIME判断上传的文件是不是图片类型
                if((src).indexOf("image/")==-1){
                    $.modal.alertWarning("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");                    
                } else {
                    $("#yl").attr("src",src);
                }
            }
            reader.readAsDataURL(this.files[0]);
        })
        
       //确定图片上传的话......
       $("#upload").click(function(){
    	   //判断文件是否为空
    	   var file=$("#coursePicture")[0];
    	   if(file.value==null || file.value==''){
    		   $.modal.alertWarning("请先选择上传的图片,图片类型,如：JPG，PNG后缀的文件。");   
    		   return;    		   
    	   }    	   
    	   var formdata = new FormData();
    	   formdata.append("coursePicture",file.files[0]);
    	   $.ajax({
    	        url: prefix + "/uploadImg",
    	        data: formdata,
    	        type: "post",
    	        processData: false,
    	        contentType: false,
    	        success: function(result) {    	        	
    	        	if(result.code=='0'){    
    	        		$.modal.alertWarning("上传图片成功......");
    	        		$("#picture").val(result.msg);    	        		
    	        	}else{    	        		
    	        		$.modal.alertWarning("上传图片失败！请重新上传图片,图片类型如：JPG，PNG后缀的文件。");
    	        	}    	        	
    	        }
    	    })   	   
       });       
        
        
    </script>
</body>
</html>